# encoding: utf-8

import requests
import json
import datetime
import os
from urllib.parse import quote
from widget.info_box import *
from widget.utils.my_threading_part import MyThreadFunc
# from widget.daoru_page.import_init_page.sqlserver_to_essystem_table__spical_source_init_window import SpicalSourceInitWindow
from widget.db_init.sqlUtils.sqlserverUtils import SqlServerOp
from widget.utils.utils import MyUtils
import time
import pandas as pd


class ImportProgressBox(ProgressBox):
    def __init__(self, my_parent, sum_num):
        super(ImportProgressBox, self).__init__(sum_num)
        self.my_parent = my_parent
        # self.bind_event()

    def closeEvent(self, a0: QCloseEvent) -> None:
        self.my_parent.parent().refresh_table(pageno=0)

    # def bind_event(self):
    #     self.cancel_btn.clicked.connect(self.my_parent.parent().retries_task_func())
    #     self.close_btn.clicked.connect()


class ProgressSignal(QObject):
    '''
    进度条自定义信号
    '''
    # 进度条改变信号
    data_changed = pyqtSignal(int, int, int, name="dataChanged")

    # 导入完成信号
    data_finished = pyqtSignal(bool, name="dataFinished")


class BaseImportFunc(object):
    '''
    sqlserver导入es的方法
    初始化的时候传入 父控件，来源信息字典，目标信息字典
    '''
    def __init__(self, source_dict, target_dict):
        self.speed = 25   # 导入速度，每次多少条
        # self.speed = 1  # 导入速度，每次多少条
        # self.table1 = cur_table
        self.source_dict = source_dict
        self.target_dict = target_dict
        self.task_dict = {}
        # 查重键值对字典
        self.check_repeat = {
            # '不查重': '',
            '跳过': 'JumpExist',
            '追加': 'App',
            '覆盖': 'Rep',
        }

    def operate_dict_init(self, cur_table):
        operate_dict = {}
        operate_list = []

        # 判断用户是否选择了查重字段
        operate_dict['check_flag'] = False
        for i in range(cur_table.rowCount()):
            if cur_table.cellWidget(i, 3) and cur_table.cellWidget(i, 3).isChecked():
                # 如果选择了查重字段，那么operate_dict['check_flag'] 置为 True
                operate_dict['check_flag'] = True
        # 如果选择了查重字段，那么继续进行重复后的处理
        for i in range(cur_table.rowCount()):
            if cur_table.cellWidget(i, 1) and cur_table.cellWidget(i, 1).currentText() == '不导入':
                continue

            # 如果选择了 '使用数据源字段名' ，那么 target_code == source_code ------------------------
            source_code = cur_table.item(i, 0).text()
            target_name = cur_table.cellWidget(i, 1).currentText()
            target_code = target_name.split('[')[1][:-1] if target_name != '使用数据源字段名' else source_code
            # 如果选择了 '使用数据源字段名' ，那么 target_code == source_code ------------------------

            op_item_dict = {
                'source_code': source_code,
                'target_code': target_code,
                'check_func': self.check_repeat[cur_table.cellWidget(i, 4).currentText()] if cur_table.cellWidget(i, 4) else None,
            }
            # print('op_item_dict: {0}'.format(op_item_dict))
            operate_list.append(op_item_dict)

        operate_dict['operate_list'] = operate_list
        repeat_check_list = []
        for i in range(cur_table.rowCount()):
            if cur_table.cellWidget(i, 3) and cur_table.cellWidget(i, 3).isChecked():
                repeat_check_list.append(cur_table.cellWidget(i, 1).currentText().split('[')[1][:-1])
        operate_dict['repeat_check_list'] = repeat_check_list
        print('operate_dict: {0}'.format(operate_dict))
        return operate_dict

    # 创建任务方法 start -------------------------------------------------------------------------
    def create_task(self, taskname, current_choice, operate_dict, related_field_dict=None):
        '''创建导入资料任务'''
        if current_choice == 1:
            # 生成任务data start  -------------------------------------------------
            metadata_list = []
            for item in operate_dict['operate_list']:
                # if item['check_func'] == '不查重':
                #     continue
                metadata_item = {}
                metadata_item['code'] = item['target_code']
                metadata_item['value'] = item['check_func']
                metadata_list.append(metadata_item)

            config = {
                "datamodelcode": self.target_dict['target_table_code'],
                "normalmsg": [{
                    "batchnum": str(int(time.time() * 1000)),  # 当前时间戳（精确到毫秒）--- 批次号
                    "wfstatus": qApp.cx_tgt['import_status'],  # 导入目标配置页选择【导入后是否可见，选是就是A选否就是N】
                    "7": qApp.cx_tgt['import_status'],  # 导入目标配置页选择【导入后是否可见，选是就是A选否就是N】

                    # ----- 特殊部分 中科供应商参数 -------------------------
                    "supplycustomer": qApp.cx_tgt['supplycustomer']
                    # ----- 特殊部分 中科供应商参数 -------------------------

                }],
                # 字段设置
                "metadata": metadata_list,
                "checkrepeat": [','.join(operate_dict['repeat_check_list']), ] if operate_dict['check_flag'] else []
            }

            # 导入配置
            fileinit = {
                'source_dict': self.source_dict,
                'target_dict': self.target_dict,
                'operate_dict': operate_dict
            }

            self.task_dict = {
                "RUID": MyUtils.get_ruid(),                                     # 任务唯一索引
                "taskname": taskname,                                           # 任务名称
                "applicationid": qApp.cur_application_dict['applicationid'],    # 系统名
                "customercode": qApp.cur_custom_dict['customercode'],           # 客户id
                # 导入配置(先把字典转成json，然后进行url编码)
                "config": quote(json.dumps(config, ensure_ascii=False)),
                'fileinit': quote(json.dumps(fileinit, ensure_ascii=False))
            }
            # 生成任务data end  -------------------------------------------------

            print('RUID:{0}'.format(self.task_dict['RUID']))
            print('json_task: {0}'.format(self.task_dict))
            # print('json_task: {0}'.format(
            #     {"RUID": MyUtils.get_ruid(), "config": quote(json.dumps(config, ensure_ascii=False))}))
            # todo
            # print(1/0)
            # url = qApp.cx_es_db_ip + '/import/createdatatask'
            url = qApp.cx_es_db_ip + '/dataprocessingapi/dataimport/createdataorappendixtask.action'
            print(url)
            res = requests.post(url, data=self.task_dict, verify=False)
            print('task_res: {0}'.format(res.text))
            field_dict = json.loads(res.text) if res.text else None
            if not field_dict:
                print("服务器估计挂了")
                return_dict = {'state': 500, 'cxajaxerrmsg': ''}
                return return_dict
            # 请求结果状态
            state = field_dict['cxajaxrc']
            print('state: {0}'.format(state))
            if state == '-1':
                print('请求成功，返回错误：{0}'.format(field_dict['cxajaxerrmsg']))
                return_dict = {'state': -1, 'cxajaxerrmsg': field_dict['cxajaxerrmsg']}
                return return_dict

            if state == '0':
                print('请求成功，返回正确，任务创建成功')
                return_dict = {'state': 0, 'RUID': self.task_dict['RUID']}
                return return_dict

        # 附件任务
        if current_choice == 3:
            # 生成任务data start  -------------------------------------------------
            config = {
                "datamodelcode": 'nullcode',   # 随便传
                "normalmsg": [{
                    "batchnum": str(int(time.time() * 1000)),  # 当前时间戳（精确到毫秒）--- 批次号
                    "wfstatus": qApp.cx_tgt['import_status'],  # 导入目标配置页选择【导入后是否可见，选是就是A选否就是N】
                    "7": qApp.cx_tgt['import_status']  # 导入目标配置页选择【导入后是否可见，选是就是A选否就是N】
                }],
                # 字段设置
                "metadata": [],    # 附件不查重穿个空就行
                "checkrepeat": []    # 附件不查重穿个空就行
            }

            # 导入配置
            fileinit = {
                'source_dict': self.source_dict,
                'target_dict': self.target_dict,
                'operate_dict': operate_dict,
                'related_field_dict': related_field_dict
            }

            self.task_dict = {
                "RUID": MyUtils.get_ruid(),  # 任务唯一索引
                "taskname": taskname,  # 任务名称
                "applicationid": qApp.cur_application_dict['applicationid'],  # 系统名
                "customercode": qApp.cur_custom_dict['customercode'],  # 客户id
                # 导入配置(先把字典转成json，然后进行url编码)
                "config": quote(json.dumps(config, ensure_ascii=False)),
                'fileinit': quote(json.dumps(fileinit, ensure_ascii=False))
            }
            # 生成任务data end  -------------------------------------------------

            print('RUID:{0}'.format(self.task_dict['RUID']))
            print('json_task: {0}'.format(self.task_dict))
            # print('json_task: {0}'.format({"RUID": MyUtils.get_ruid(), "config": config}))
            # print('json_task: {0}'.format({"RUID": MyUtils.get_ruid(), "config": quote(json.dumps(config, ensure_ascii=False))}))
            # print(1/0)
            # url = qApp.cx_es_db_ip + '/import/createdatatask'
            url = qApp.cx_es_db_ip + '/dataprocessingapi/dataimport/createdataorappendixtask.action'
            print(url)
            res = requests.post(url, data=self.task_dict, verify=False)
            print("res.text: {0}".format(res.text))
            field_dict = json.loads(res.text) if res.text else None
            if not field_dict:
                print("服务器估计挂了")
                return_dict = {'state': 500, 'cxajaxerrmsg': ''}
                return return_dict
            # 请求结果状态
            state = field_dict['cxajaxrc']
            print('state: {0}'.format(state))
            if state == '-1':
                print('请求成功，返回错误：{0}'.format(field_dict['cxajaxerrmsg']))
                return_dict = {'state': -1, 'cxajaxerrmsg': field_dict['cxajaxerrmsg']}
                return return_dict

            if state == '0':
                print('请求成功，返回正确，任务创建成功')
                return_dict = {'state': 0, 'RUID': self.task_dict['RUID']}
                return return_dict

        # 创建实体任务
        if current_choice == 2:
            # 生成任务data start  -------------------------------------------------
            metadata_list = []
            for item in operate_dict['operate_list']:
                # if item['check_func'] == '不查重':
                #     continue
                metadata_item = {}
                metadata_item['name'] = item['target_code']
                metadata_item['value'] = item['check_func']
                metadata_list.append(metadata_item)

            config = {
                "entitytype": self.target_dict['target_table_code'],
                # 字段设置
                "field": metadata_list,
                "checkrepeat": [','.join(operate_dict['repeat_check_list']), ] if 'repeat_check_list' in operate_dict and operate_dict['repeat_check_list'] else None
            }

            # 导入配置
            fileinit = {
                'source_dict': self.source_dict,
                'target_dict': self.target_dict,
                'operate_dict': operate_dict
            }

            self.task_dict = {
                "batchno": str(int(time.time() * 1000)),  # 当前时间戳（精确到毫秒）--- 实体批次号
                "RUID": MyUtils.get_ruid(),
                "taskname": taskname,  # 任务名称
                "applicationid": qApp.cur_application_dict['applicationid'],  # 系统名
                "customercode": qApp.cur_custom_dict['customercode'],  # 客户id
                # 导入配置(先把字典转成json，然后进行url编码)
                "config": quote(json.dumps(config, ensure_ascii=False)),
                'fileinit': quote(json.dumps(fileinit, ensure_ascii=False))
            }
            # 生成任务data end  -------------------------------------------------

            print('RUID:{0}'.format(self.task_dict['RUID']))
            print('json_task: {0}'.format(self.task_dict))
            # print(1/0)
            # url = qApp.cx_es_db_ip + '/import/createentitytask'
            url = qApp.cx_es_db_ip + '/dataprocessingapi/dataimport/createentitytask.action'
            print(url)
            res = requests.post(url, data=self.task_dict, verify=False)
            print(res)
            field_dict = json.loads(res.text) if res.text else None
            if not field_dict:
                print("服务器估计挂了")
                return_dict = {'state': 500, 'cxajaxerrmsg': ''}
                return return_dict
            # 请求结果状态
            state = field_dict['cxajaxrc']
            print('state: {0}'.format(state))
            if state == '-1':
                print('请求成功，返回错误：{0}'.format(field_dict['cxajaxerrmsg']))
                return_dict = {'state': -1, 'cxajaxerrmsg': field_dict['cxajaxerrmsg']}
                return return_dict

            if state == '0':
                print('请求成功，返回正确，任务创建成功')
                return_dict = {'state': 0, 'RUID': self.task_dict['RUID']}
                return return_dict
    # 创建任务方法 end -------------------------------------------------------------------------

    # 导入资料方法 start -------------------------------------------------------------------------
    def start_import_ziliao(self, all_data, RUID, operate_dict, table_parent_widget):
        '''
        开始导入(资料部分)
        :return:
        '''
        print('加载导入数据...')

        # all_data = self.get_all_data(operate_dict)

        # print(len(all_data))
        # print('all_data: {0}'.format(all_data))
        # print(1/0)

        # 消息盒子start -------------------------------------------
        table_parent_widget.progress_box = ImportProgressBox(my_parent=table_parent_widget, sum_num=len(all_data))

        # self.table1.parent().progress_box.setWindowFlag(Qt.WindowStaysOnTopHint)
        # def stop_run():
        #     self.table1.parent().run_flag = False
        # 关闭按钮点击，终止线程
        # self.table1.parent().progress_box.close_btn.clicked.connect(stop_run)
        # 取消按钮点击，关闭窗口
        table_parent_widget.progress_box.show()

        # 初始化进度条信号实例
        self.sig = ProgressSignal()
        # 绑定更新进度槽
        self.sig.dataChanged.connect(table_parent_widget.progress_box.update_progress)
        # 绑定导入结束槽
        self.sig.dataFinished.connect(table_parent_widget.progress_box.import_finished)

        # 消息盒子end --------------------------------------------

        # 展示进度
        def start_run():
            data_dict_list = []
            for i, field in enumerate(all_data):
                field = list(field)  # 元祖转字典
                data_dict = {}

                # 插入一对一元数据 start -----------------------------------------------
                for k, item in enumerate(operate_dict['operate_list']):
                    # if k != 10100001:
                    if not field[k]:
                        # print('field[k]: {0}-{1}'.format(k, field[k]))
                        if item['target_code'] != '10100001':
                            continue
                        else:
                            field[k] = ' '
                            # print(1/0)
                    data_dict[item['target_code']] = field[k]

                data_dict_list.append(data_dict)
                # print('data_dict: {0}'.format(data_dict))

                if (i + 1) % self.speed == 0 or (i + 1) == len(all_data):
                    '''
                    {
                        "RUID": "任务唯一标识",
                        "data": [
                          {"RUID":"","10100001":"test","1590001":["1","2"]}
                        ]
                    }
                    '''
                    data_dict_encoding = quote(json.dumps(data_dict_list, ensure_ascii=False))
                    # print('data_dict_encoding: {0}'.format(data_dict_encoding))

                    # 导入数据格式 start --------------------------------
                    import_dict = {
                        "RUID": RUID,
                        "data": data_dict_encoding,
                        "appendix": quote(json.dumps([], ensure_ascii=False))
                        # "data": [data_dict]
                    }
                    # 导入数据格式 end --------------------------------

                    # 进度(第一次/重新传递数据传递start,最后一批数据传递finish，中间不传递)
                    if (i + 1) == self.speed:
                        import_dict['schedule'] = 'start'
                    if (i + 1) == len(all_data):
                        import_dict['schedule'] = 'finish'

                    print('import_dict: {0}'.format(import_dict))

                    # url = qApp.cx_es_db_ip + '/import/importdata'
                    # url = qApp.cx_es_db_ip + '/import/importdataorappendix'
                    url = qApp.cx_es_db_ip + '/dataprocessingapi/dataimport/importdataorappendix.action'
                    print(url)
                    print('请求导入。。。')
                    print('当前任务RUID:{0}'.format(RUID))
                    res = requests.post(url, data=import_dict, verify=False)
                    # print('请求返回信息res：{0}'.format(res.text))
                    # print('请求返回。。。')
                    print('后台返回值：{0}'.format(res.status_code))
                    print('当前进度： {0}'.format(i))

                    if res.status_code == 500:
                        print('报错信息{0}：'.format(res.text))
                        return
                        # return_dict = {'state': 500}
                        # return return_dict
                    field_dict = json.loads(res.text) if res.text else None
                    # print(field_dict)
                    # 请求结果状态
                    state = field_dict['cxajaxrc']
                    print('state: {0}'.format(state))
                    print('field_dict: {0}'.format(field_dict))
                    if state == '-1':
                        log_str = '资料进度：{0}；请求成功，返回-1，报错信息：{1}'.format(i, field_dict['cxajaxerrmsg'] if 'cxajaxerrmsg' in field_dict else '')
                        self.add_err_log(log_str)
                        print('请求成功，返回错误：{0}'.format(field_dict['cxajaxerrmsg']))
                    if state == '0':
                        # 插入日志 start -----------------------------------------------------
                        try:
                            returnvalue = field_dict['returnvalue']
                            print('returnvalue: {0}'.format(returnvalue))
                            if 'cxajaxloglist' in returnvalue:
                                log_str = ''
                                for u in range(len(returnvalue['cxajaxloglist'])):
                                    cur_num = (i + 1) - 25 + (u + 1)
                                    log_str += '资料进度：{0}-{1}；请求成功，返回0，导入信息：{2}\n'.format(
                                        cur_num,
                                        data_dict_list[u]['10100001'],
                                        str(returnvalue['cxajaxloglist'][u] if returnvalue['cxajaxloglist'][
                                            u] else '导入成功')
                                    )
                                self.add_err_log(log_str)
                        except Exception as e:
                            print(e)
                        # 插入日志 end -----------------------------------------------------

                        print('请求成功，返回正确')
                        # self.add_err_log('资料进度：{0}；请求成功，返回0，报错信息：{1}'.format(i, str(field_dict['cxajaxloglist'] if 'cxajaxloglist' in field_dict else '')))
                        # print('请求成功，返回正确')
                        # print('data_dict_encoding: {0}'.format(data_dict_encoding))
                        # print('data_dict: {0}'.format(json.dumps(data_dict_list, ensure_ascii=False)))
                        # print(111111111111111111111111111111111111111111)
                        success_num = field_dict['returnvalue']["success"]
                        fail_num = field_dict['returnvalue']["error"]
                        # cal_signal = pyqtSignal(np.ndarray, np.ndarray)

                        # self.download_proess_signal.emit(int(proess))

                        # 发射进度条更新信号
                        self.sig.dataChanged.emit(success_num, fail_num, i + 1)

                        # self.table1.parent().progress_box.update_progress(success_num, fail_num)

                        # print('成功{0}条'.format(success_num))
                        # print('失败{0}条'.format(fail_num))
                        # return_dict = {'state': -1, 'success_num': success_num, 'fail_num': fail_num}
                        # return return_dict

                    # 一百天传完后，清空队列，再传下一百条
                    data_dict_list = []

            # 发射导入结束信号
            self.sig.dataFinished.emit(True)

        # _thread.start_new_thread(start_run, ())

        self.import_thread = MyThreadFunc(start_run)
        self.import_thread.start()
        # 关闭按钮点击，终止线程
        table_parent_widget.progress_box.close_btn.clicked.connect(self.import_thread.stop)
        # table_parent_widget.progress_box.close_btn.clicked.connect(lambda: table_parent_widget.parent().retries_task_func(RUID))
        # table_parent_widget.progress_box.close_btn.clicked.connect(lambda: table_parent_widget.parent().refresh_table(pageno=0))
        table_parent_widget.progress_box.cancel_btn.clicked.connect(table_parent_widget.progress_box.close_btn.click)
    # 导入资料方法 end -------------------------------------------------------------------------

    # 导入附件方法 start -------------------------------------------------------------------------
    def start_import_fujian(self, all_data, RUID, operate_dict, table_parent_widget, related_field_dict):
        '''
        开始导入(附件单独方法)
        :return:
        '''
        # # 获取路径ruid -----------------------
        # book_code = 1987051001
        # url = qApp.cx_es_db_ip + '/import/metadatalist?datamodel={0}'.format(book_code)
        # print(url)
        # res = requests.get(url, verify=False)
        # # print(res.text)
        # field_dict = json.loads(res.text) if res.text else None
        # re_path_ruid = [field['vdir'] for field in field_dict['metadata'] if field['code'] == 350050001][0]
        # print('路径ruid: {0}'.format(re_path_ruid))
        # # 获取路径ruid -----------------------

        print('加载导入数据...')
        # pf_code = {
        #     'p_ruid': 'RUID',
        #     'f_ruid': 'doruid',
        # }
        # print('{0} {1} {2} {3} {4}'.format(self.source_dict['server_name'], self.source_dict['port'], self.source_dict['db_name'], self.source_dict['username'], self.source_dict['password']))
        # sql = """SELECT {0}, {1} FROM {2} WHERE f_ruid='jmu_sqlserver_no1_cjg2cxstar_cjg_book_72000002'""".format(
        #     ','.join([source_field['source_code'] for source_field in self.operate_dict['operate_list']]),
        #     ','.join(qApp.cx_source_sqlserver_dict['field_pf_name_list']),
        #     self.source_dict['table_name']
        # )

        # all_data = self.get_all_data(operate_dict, related_field_dict)

        # print(len(all_data))
        # print('all_data: {0}'.format(all_data))
        # print(1/0)

        # 消息盒子start -------------------------------------------
        table_parent_widget.progress_box = ImportProgressBox(my_parent=table_parent_widget, sum_num=len(all_data))

        # self.table1.parent().progress_box.setWindowFlag(Qt.WindowStaysOnTopHint)
        # def stop_run():
        #     self.table1.parent().run_flag = False
        # 关闭按钮点击，终止线程
        # self.table1.parent().progress_box.close_btn.clicked.connect(stop_run)
        # 取消按钮点击，关闭窗口
        table_parent_widget.progress_box.show()

        # 初始化进度条信号实例
        self.sig = ProgressSignal()
        # 绑定更新进度槽
        self.sig.dataChanged.connect(table_parent_widget.progress_box.update_progress)
        # 绑定导入结束槽
        self.sig.dataFinished.connect(table_parent_widget.progress_box.import_finished)

        # 消息盒子end --------------------------------------------

        # 展示进度
        def start_run():
            data_dict_list = []
            for i, field in enumerate(all_data):
                # 如果运行flag为False 就跳出
                # if not self.table1.parent().run_flag:
                #     print('线程终止')
                #     return

                data_dict = {
                    "field": {},
                    "relateddatatype": self.target_dict['target_table_code'],
                    "relatedfield": {}
                }
                # 插入一对一元数据 start -----------------------------------------------
                for k, item in enumerate(operate_dict['operate_list']):
                    if not field[k]:
                        continue
                    data_dict["field"][item['target_code']] = field[k]

                # 插入附件与资料的关联字段
                for j, key in enumerate(related_field_dict):
                    data_dict["relatedfield"][related_field_dict[key]] = field[len(field) - len(related_field_dict) + j]

                # 插入固定值 start -------------------------------------------------
                # data_dict['wfstatus'] = 'A'
                # # data_dict['7'] = 'A'
                # data_dict['customercode'] = '1878'
                # data_dict['siteid'] = 'urmp'
                # # data_dict['20100001'] = 'publicpost'    # 保密级别
                # data_dict['mdcode'] = '350050001'
                # data_dict['vdir'] = re_path_ruid

                # 附件的特殊处理 start --------------------------------------------
                # 判断，如果 data_dict字典中没有 ext键，那么将 filename通过 “.” 拆分成 filename 和 ext
                if not 'ext' in data_dict['field'] and '.' in data_dict['field']['filename']:
                    # 文件名截取
                    old_filename = data_dict['field']['filename']
                    data_dict['field']['filename'] = old_filename.split('.')[0]
                    data_dict['field']['ext'] = old_filename.split('.')[1]
                if 'ext' in data_dict['field'] and 'name' in data_dict['field'] and '.' in data_dict['field']['name']:
                    # 显示文件名截取
                    old_name = data_dict['field']['name']
                    data_dict['field']['name'] = old_name.split('.')[0]
                # path 的值赋给 path0
                if data_dict['field']['path']:
                    data_dict['field']['path0'] = data_dict['field']['path']
                # 附件的特殊处理 start --------------------------------------------

                # 插入固定值 end -------------------------------------------------

                data_dict_list.append(data_dict)
                # print('data_dict: {0}'.format(data_dict))
                # print(1 / 0) ------------

                # 每过 self.speed 条数据发送一次请求
                # self.add_err_log('附件进度：{0}'.format(i))
                # print(i)
                if (i + 1) % self.speed == 0 or (i + 1) == len(all_data):
                    '''
                    {
                        "RUID": "任务唯一标识",
                        "data": [
                          {"RUID":"","10100001":"test","1590001":["1","2"]}
                        ]
                    }
                    '''
                    # data_dict = [{"RUID": "", "10100001": "test", "1590001": ["1", "2"]}]
                    print('data_dict_list: {0}'.format(data_dict_list))
                    data_dict_encoding = quote(json.dumps(data_dict_list, ensure_ascii=False))
                    # import_dict = {
                    #     # "RUID": self.task_dict['RUID'],
                    #     "data": data_dict_encoding
                    #     # "data": [data_dict]
                    # }

                    # 导入数据格式 start --------------------------------
                    import_dict = {
                        "RUID": RUID,
                        "data": quote(json.dumps([], ensure_ascii=False)),
                        "appendix": data_dict_encoding
                        # "data": [data_dict]
                    }
                    # 导入数据格式 end --------------------------------

                    # 进度(第一次/重新传递数据传递start,最后一批数据传递finish，中间不传递)
                    if (i + 1) == self.speed:
                        import_dict['schedule'] = 'start'
                    if (i + 1) == len(all_data):
                        import_dict['schedule'] = 'finish'

                    # print({"data": data_dict_list})
                    # return
                    # print(1/0)
                    print('import_dict: {0}'.format(import_dict))

                    # url = qApp.cx_es_db_ip + '/import/importdofile6'
                    # url = qApp.cx_es_db_ip + '/import/importdataorappendix'
                    url = qApp.cx_es_db_ip + '/dataprocessingapi/dataimport/importdataorappendix.action'
                    print(url)
                    # print(2211111)
                    res = requests.post(url, data=import_dict, verify=False)
                    # print(2222222)
                    print('res.text: {0}'.format(res.text))
                    field_dict = json.loads(res.text) if res.text else None
                    print('返回内容： {0}'.format(field_dict))
                    if not field_dict:
                        print("服务器估计挂了")
                        # return_dict = {'state': 500}
                        # return return_dict
                    # 请求结果状态
                    state = field_dict['cxajaxrc']
                    print('state: {0}'.format(state))
                    if state == '-1':
                        self.add_err_log('附件进度：{0}；请求成功，返回-1，报错信息：{1}'.format(i, field_dict[
                            'cxajaxerrmsg'] if 'cxajaxerrmsg' in field_dict else ''))
                        print('请求成功，返回错误：{0}'.format(field_dict['cxajaxerrmsg']))
                    if state == '0':
                        # returnvalue = field_dict['returnvalue']
                        # if 'cxajaxloglist' in returnvalue:
                        #     self.add_err_log('附件进度：{0}；请求成功，返回0，报错信息：{1}'.format(i, str(returnvalue['cxajaxloglist'] if 'cxajaxloglist' in returnvalue else '')))

                        # 插入日志 start -----------------------------------------------------
                        try:
                            returnvalue = field_dict['returnvalue']
                            if 'cxajaxloglist' in returnvalue:
                                log_str = ''
                                for u in range(len(returnvalue['cxajaxloglist'])):
                                    cur_num = (i + 1) - 25 + (u + 1)
                                    log_str += '附件进度：{0}；请求成功，返回0，导入信息：{1}\n'.format(
                                        cur_num,
                                        # data_dict_list[u]['10100001'],
                                        str(returnvalue['cxajaxloglist'][u] if returnvalue['cxajaxloglist'][
                                            u] else '导入成功')
                                    )
                                self.add_err_log(log_str)
                        except Exception as e:
                            print(e)
                        # 插入日志 end -----------------------------------------------------

                        print('请求成功，返回正确')
                        # self.add_err_log('资料进度：{0}；请求成功，返回0，报错信息：{1}'.format(i, str(field_dict['cxajaxloglist'] if 'cxajaxloglist' in field_dict else '')))
                        # print('请求成功，返回正确')
                        success_num = field_dict['returnvalue']["dofile6successNum"]
                        fail_num = field_dict['returnvalue']["dofile6errorNum"]
                        # cal_signal = pyqtSignal(np.ndarray, np.ndarray)

                        # self.download_proess_signal.emit(int(proess))

                        # 发射进度条更新信号
                        self.sig.dataChanged.emit(success_num, fail_num, i + 1)

                        # self.table1.parent().progress_box.update_progress(success_num, fail_num)

                        # print('成功{0}条'.format(success_num))
                        # print('失败{0}条'.format(fail_num))
                        # return_dict = {'state': -1, 'success_num': success_num, 'fail_num': fail_num}
                        # return return_dict

                    # 一百天传完后，清空队列，再传下一百条
                    data_dict_list = []

            # 发射导入结束信号
            self.sig.dataFinished.emit(True)

        # _thread.start_new_thread(start_run, ())

        self.import_thread = MyThreadFunc(start_run)
        self.import_thread.start()
        # 关闭按钮点击，终止线程
        table_parent_widget.progress_box.close_btn.clicked.connect(self.import_thread.stop)
        table_parent_widget.progress_box.cancel_btn.clicked.connect(table_parent_widget.progress_box.close_btn.click)

    # 导入附件方法 end -------------------------------------------------------------------------

    # 导入实体方法 start -------------------------------------------------------------------------
    def start_import_shiti(self, all_data, RUID, operate_dict, table_parent_widget):
        '''
        开始导入(实体部分)
        :return:
        '''
        print('加载导入数据...')

        # all_data = self.get_all_data(operate_dict)
        # pf_code = {
        #     'p_ruid': 'RUID',
        #     'f_ruid': 'doruid',
        # }
        # # print('{0} {1} {2} {3} {4}'.format(self.source_dict['server_name'], self.source_dict['port'], self.source_dict['db_name'], self.source_dict['username'], self.source_dict['password']))
        # sop = SqlServerOp(self.source_dict['uni']['server_name'], self.source_dict['uni']['port'], self.source_dict['uni']['db_name'], self.source_dict['uni']['username'], self.source_dict['uni']['password'])
        # sql = """SELECT {0}{1}{2} FROM {3}""".format(
        #     ','.join([source_field['source_code'] for source_field in self.operate_dict['operate_list']]),
        #     ',' if qApp.cx_src['uni']['field_pf_name_list'] else '',
        #     ','.join(qApp.cx_src['uni']['field_pf_name_list']),
        #     self.source_dict['uni']['table_name']
        # )
        # print('sql: {0}'.format(sql))
        # all_data = sop.select(sql)

        # print(len(all_data))
        # print('all_data: {0}'.format(all_data))
        # print(1/0)

        # 消息盒子start -------------------------------------------
        table_parent_widget.progress_box = ImportProgressBox(my_parent=table_parent_widget, sum_num=len(all_data))

        # self.table1.parent().progress_box.setWindowFlag(Qt.WindowStaysOnTopHint)
        # def stop_run():
        #     self.table1.parent().run_flag = False
        # 关闭按钮点击，终止线程
        # self.table1.parent().progress_box.close_btn.clicked.connect(stop_run)
        # 取消按钮点击，关闭窗口
        table_parent_widget.progress_box.show()

        # 初始化进度条信号实例
        self.sig = ProgressSignal()
        # 绑定更新进度槽
        self.sig.dataChanged.connect(table_parent_widget.progress_box.update_progress)
        # 绑定导入结束槽
        self.sig.dataFinished.connect(table_parent_widget.progress_box.import_finished)

        # 消息盒子end --------------------------------------------

        # 展示进度
        def start_run():
            data_dict_list = []
            for i, field in enumerate(all_data):
                # 如果运行flag为False 就跳出
                # if not self.table1.parent().run_flag:
                #     print('线程终止')
                #     return

                data_dict = {}

                # 插入一对一元数据 start -----------------------------------------------
                for k, item in enumerate(operate_dict['operate_list']):
                    if not field[k]:
                        continue
                    data_dict[item['target_code']] = field[k]

                # 插入固定值 start -------------------------------------------------
                # data_dict['wfstatus'] = 'A'
                # data_dict['7'] = 'A'
                # data_dict['customercode'] = '1878'
                # data_dict['siteid'] = 'urmp'
                # data_dict['20100001'] = 'publicpost'    # 保密级别
                # 插入主键和当前表对应值
                # for j, dt in enumerate(qApp.cx_src['uni']['field_pf_name_list']):
                #     pf_num = len(self.operate_dict['operate_list']) + j
                #     data_dict[pf_code[dt]] = field[pf_num]
                #     print(field)
                # 插入固定值 end -------------------------------------------------

                data_dict_list.append(data_dict)
                # print('data_dict: {0}'.format(data_dict))
                # print(1/0)
                # return

                # 每过 self.speed 条数据发送一次请求
                # self.add_err_log('实体进度：{0}'.format(i))
                # print(i)
                if (i + 1) % self.speed == 0 or (i + 1) == len(all_data):
                    '''
                    {
                        "RUID": "任务唯一标识",
                        "record": [
                          {"RUID":"","10100001":"test","1590001":["1","2"]}
                        ]
                    }
                    '''
                    # data_dict = [{"RUID": "", "10100001": "test", "1590001": ["1", "2"]}]

                    data_dict_encoding = quote(json.dumps(data_dict_list, ensure_ascii=False))
                    import_dict = {
                        "RUID": RUID,
                        "record": data_dict_encoding
                        # "data": [data_dict]
                    }

                    # 进度(第一次/重新传递数据传递start,最后一批数据传递finish，中间不传递)
                    if (i + 1) == self.speed:
                        import_dict['schedule'] = 'start'
                    if (i + 1) == len(all_data):
                        import_dict['schedule'] = 'finish'

                    # print({"RUID": RUID, "record": data_dict_list})
                    # print(1/0)
                    # return
                    print('import_dict: {0}'.format(import_dict))

                    # url = qApp.cx_es_db_ip + '/import/importentity'
                    # url = qApp.cx_es_db_ip + '/import/importentity'
                    url = qApp.cx_es_db_ip + '/dataprocessingapi/dataimport/importentity.action'
                    print(url)
                    print('发送请求。。。')
                    res = requests.post(url, data=import_dict, verify=False)
                    print('请求完毕。。。')
                    print('请求返回信息res：{0}'.format(res.text))
                    field_dict = json.loads(res.text) if res.text else None
                    if not field_dict:
                        print("服务器估计挂了")
                        # return_dict = {'state': 500}
                        # return return_dict
                    # 请求结果状态
                    state = field_dict['cxajaxrc']
                    print('state: {0}'.format(state))
                    if state == '-1':
                        self.add_err_log('基础表进度：{0}；请求成功，返回-1，报错信息：{1}'.format(i, field_dict[
                            'cxajaxerrmsg'] if 'cxajaxerrmsg' in field_dict else ''))
                        print('请求成功，返回错误：{0}'.format(field_dict['cxajaxerrmsg']))
                    if state == '0':
                        # returnvalue = field_dict['returnvalue']
                        # if 'cxajaxloglist' in returnvalue:
                        #     self.add_err_log('基础表进度：{0}；请求成功，返回0，报错信息：{1}'.format(i, str(
                        #         returnvalue['cxajaxloglist'] if 'cxajaxloglist' in returnvalue else '')))

                        # 插入日志 start -----------------------------------------------------
                        try:
                            returnvalue = field_dict['returnvalue']
                            if 'cxajaxloglist' in returnvalue:
                                log_str = ''
                                for u in range(len(returnvalue['cxajaxloglist'])):
                                    cur_num = (i + 1) - 25 + (u + 1)
                                    log_str += '实体进度：{0}；请求成功，返回0，导入信息：{1}\n'.format(
                                        cur_num,
                                        # data_dict_list[u]['10100001'],
                                        str(returnvalue['cxajaxloglist'][u] if returnvalue['cxajaxloglist'][
                                            u] else '导入成功')
                                    )
                                self.add_err_log(log_str)
                        except Exception as e:
                            print(e)
                        # 插入日志 end -----------------------------------------------------

                        print('请求成功，返回正确')
                        success_num = field_dict['returnvalue']["success"]
                        fail_num = field_dict['returnvalue']["error"]
                        # cal_signal = pyqtSignal(np.ndarray, np.ndarray)

                        # self.download_proess_signal.emit(int(proess))

                        # 发射进度条更新信号
                        self.sig.dataChanged.emit(success_num, fail_num, i + 1)

                        # self.table1.parent().progress_box.update_progress(success_num, fail_num)

                        # print('成功{0}条'.format(success_num))
                        # print('失败{0}条'.format(fail_num))
                        # return_dict = {'state': -1, 'success_num': success_num, 'fail_num': fail_num}
                        # return return_dict

                    # 一百天传完后，清空队列，再传下一百条
                    data_dict_list = []

            # 发射导入结束信号
            self.sig.dataFinished.emit(True)

        # _thread.start_new_thread(start_run, ())

        self.import_thread = MyThreadFunc(start_run)
        self.import_thread.start()
        # 关闭按钮点击，终止线程
        table_parent_widget.progress_box.close_btn.clicked.connect(self.import_thread.stop)
        table_parent_widget.progress_box.cancel_btn.clicked.connect(table_parent_widget.progress_box.close_btn.click)

    # 导入实体方法 end -------------------------------------------------------------------------

    def add_err_log(self, msg):
        '''
        输出错误日志到 init_file/log/err.txt中
        :param msg: 日志
        :return:
        '''
        path = r'./init_file/log/'
        file_name = '批次号.csv'

        df = pd.DataFrame([[msg]], columns=['log'])

        csv_path = path + file_name
        df.to_csv(csv_path, mode='a')
        print('save_log======================================')





    # def fileTooBig(self, filePath):
    #     '''
    #     获取文件大小
    #     :param filePath: 日志路径
    #     :return:
    #     '''
    #     pass